#include "stdafx.h"
#include "Texture.h"
#include "tools/file/File.h"
#include "config/IncludeParts.h"

#if USE_BMP
#include "tools/image/BmpReader.h"
#endif

using namespace unigame;

std::map<std::string, Texture*> Texture::s_loadedTextures;

Texture::Texture(File* file, int32u format):
m_name(file->name())
{
	/*file->read();
	ImageReader* reader;
	switch(format)
	{
#if USE_BMP
	case E_BMP_IF:
		reader = uni_new BmpReader(file->getData(), file->getSize());
		break;
#endif
	case E_PNG_IF:
	case E_TGA_IF:
	default:
		assert(false && "don't support image format");
		break;
	}
	if(!reader->read())
	{
		char text[200];
		sprintf(text, "load image wrong %s", m_name.c_str());
		assert(false && text);
	}
	delete reader;
	s_loadedTextures[m_name] = this;*/
}

Texture::~Texture()
{
	s_loadedTextures.erase(s_loadedTextures.find(m_name));
}

Texture* Texture::loadTexture(const char* name, int32u type, int32u format)
{
	File* file = uni_new File(name, type);
	std::string n(file->name());
	std::map<std::string, Texture*>::iterator it = s_loadedTextures.find(n);
	Texture* tex;
	if(it == s_loadedTextures.end())
	{
		tex = uni_new Texture(file, format);
	}
	else
	{
		tex = it->second;
	}
	delete file;
	return tex;
}